"""
运行程序运行此文件
"""
import sys
from PyQt6.QtGui import *
from PyQt6.QtWidgets import *
from PyQt6 import QtCore
from selectPage import Ui_choose
from mainFace import QmyFace
from mainRec import QmyRec
from mainDetect import QmyDetect
from mainwelcom import Qmywelcom
from maindenglu import Qmydenglu
from mainpassword import Qmypassword


class Choose(QWidget):
    switch_window1 = QtCore.pyqtSignal()  # 跳转信号
    switch_window2 = QtCore.pyqtSignal()  # 跳转信号
    switch_window3 = QtCore.pyqtSignal()  # 跳转信号
    switch_psd = QtCore.pyqtSignal()  # 跳转信号

    def __init__(self, parent=None):
        super().__init__(parent)
        self.choose = Ui_choose()
        self.choose.setupUi(self)
        self.setGeometry(700, 800, 700, 800)
        self.center()
        self.setWindowTitle('比奇堡在逃海绵')
        self.choose.choose_face.clicked.connect(self.go_face)
        self.choose.choose_rec.clicked.connect(self.go_rec)
        self.choose.choose_detect.clicked.connect(self.go_detect)
        self.choose.re_psd.clicked.connect(self.go_psd_main)
        self.choose.exit.clicked.connect(QtCore.QCoreApplication.instance().quit)

    def paintEvent(self, event):
        painter = QPainter(self)
        pixmap = QPixmap("选择功能界面背景.png")
        # 绘制窗口背景，平铺到整个窗口，随着窗口改变而改变
        painter.drawPixmap(self.rect(), pixmap)

    def go_face(self):
        self.switch_window1.emit()

    def go_rec(self):
        self.switch_window2.emit()

    def go_detect(self):
        self.switch_window3.emit()

    def go_psd_main(self):
        self.switch_psd.emit()

    '''屏幕居中'''

    def center(self):
        qr = self.frameGeometry()  # 得到一个指定主窗口几何形状的矩形
        cp = self.screen().availableGeometry().center()  # 计算出显示器的分辨率，通过分辨率得出中心点
        qr.moveCenter(cp)  # 设置为屏幕的中心，矩形大小不变
        self.move(qr.topLeft())


# 利用一个控制器来控制页面的跳转
class Controller:
    def __init__(self):
        self.face = QmyFace()
        self.rec = QmyRec()
        self.detect = QmyDetect()
        self.choose = Choose()
        self.deng_lu = Qmydenglu()
        self.wel_come = Qmywelcom()
        self.psd = Qmypassword()

    # 展示欢迎页面
    def show_wel(self):
        self.choose.close()
        self.wel_come.switch_wel.connect(self.show_login)
        self.wel_come.show()

    # 跳转到 login 窗口, 注意关闭原页面
    def show_login(self):
        self.choose.close()
        self.wel_come.close()
        self.deng_lu.switch_login.connect(self.show_choose)
        self.deng_lu.show()

    # 跳转到 choose 窗口
    def show_choose(self):
        self.deng_lu.close()
        self.choose.switch_window1.connect(self.show_face)
        self.choose.switch_window2.connect(self.show_rec)
        self.choose.switch_window3.connect(self.show_detect)
        self.choose.switch_psd.connect(self.show_psd)
        self.choose.show()

    # 跳转到 face 窗口, 注意关闭原页面
    def show_face(self):
        self.choose.close()
        self.face.show()

    # 跳转到 rec 窗口, 注意关闭原页面
    def show_rec(self):
        self.choose.close()
        self.rec.show()

    # 跳转到 detect 窗口, 注意关闭原页面
    def show_detect(self):
        self.choose.close()
        self.detect.show()

    # 跳转到 psd 窗口, 注意关闭原页面
    def show_psd(self):
        self.choose.close()
        self.psd.show()


# 实现子页面返回主界面
class ControlSkip:
    def __init__(self, win):
        self.face = win.face
        self.choose = win.choose
        self.rec = win.rec
        self.detect = win.detect
        self.deng_lu = win.deng_lu
        self.wel_come = win.wel_come
        self.psd = win.psd

    # 重置密码跳转到login
    def show_psd_login(self):
        self.psd.switch_denglu.connect(self.show_login)

    # 人脸录入跳转到choose
    def show_face_main(self):
        self.face.switch_window4.connect(self.show_main)
        self.face.hide()

    # 人脸识别跳转到choose
    def show_rec_main(self):
        self.rec.switch_window5.connect(self.show_main)

    # 检测识别跳转到choose
    def show_detect_main(self):
        self.detect.switch_window6.connect(self.show_main)

    def show_main(self):
        self.face.close()
        self.rec.close()
        self.detect.close()
        self.deng_lu.close()
        self.wel_come.close()
        self.psd.close()
        self.choose.show()

    # 返回到登录页面
    def show_login(self):
        self.psd.close()
        self.deng_lu.show()


if __name__ == '__main__':
    app = QApplication(sys.argv)
    app.setWindowIcon(QIcon('菠萝屋ico.jpg'))
    controller = Controller()  # 控制器实例
    control_skip = ControlSkip(controller)
    controller.show_wel()  # 默认展示的是 wel 页面
    control_skip.show_face_main()
    control_skip.show_rec_main()
    control_skip.show_detect_main()
    control_skip.show_psd_login()
    sys.exit(app.exec())
